/*-------------------------------------------*/
//EDIT
function EditOnLoad() {
	document.getElementById('setLocation').style.display="none";	
	document.getElementById('setEvent').style.display="none";	
	document.getElementById('addItemBtn').style.display="none";		
}
var editMode = false;
var addLocation = false;
function switchEditMode() {
	if(editMode == false){
		document.getElementById('editbtn').value = "Wylacz tryb edycji";
		editMode = true;
		getMovieName("MapAdapter").edit();
	}else{
		document.getElementById('editbtn').value = "Wlacz tryb edycji";
		editMode = false;
		getMovieName("MapAdapter").cancelEdit();
	}
	EditOnLoad();	
}
var locationX;
var locationY;
//map call if click at empty point
function callbackInsertPoint(s) {
	$(s).find("x").each(function()
	{
	  locationX = $(this).text();
	});
	$(s).find("y").each(function()
	{
	  locationY = $(this).text();
	});	
	document.getElementById('setLocation').style.display="block";	
	document.getElementById('setEvent').style.display="block";	
	document.getElementById('addItemBtn').style.display="block";	
	addLocation = true;	
}
function addItem(){
	tags = "";
	select=document.getElementById('tagItem');
	for (var i=0; i<select.options.length; i++){
		if (select.options[i].selected==true){
			tags += select.options[i].value + "|";
		}
	}
	
	categores = "";
	var select=document.getElementById('categoryItem');
	for (var i=0; i<select.options.length; i++){
		if (select.options[i].selected==true){
			categores += select.options[i].value + "|";
		}
	}	
	if(addLocation){
		//add new point
		var eventLocationName = document.getElementById('eventLocationName').value;
		var eventLocationAdres=document.getElementById('eventLocationAdres').value;
		var eventLocationCity=document.getElementById('eventLocationCity').value;
		var eventLocationLocationName=document.getElementById('eventLocationLocationName').value;
		var eventLocationDescription=document.getElementById('eventLocationDescription').value;
		var eventName=document.getElementById('eventName').value;
		var eventDescription=document.getElementById('eventDescription').value;
		var eventValue=document.getElementById('eventValue').value;
		
		if(eventLocationName=="" || eventName=="" || eventValue==""){
			alert("Wypelnij brakujace dane! nazwa lokacji, nazwa przedmiotu, wartosc");
			return;
		}
		getMovieName("MapAdapter").insertEvent(eventLocationName, eventLocationAdres, eventLocationCity, eventLocationLocationName, eventLocationDescription, locationX, locationY, eventName, eventDescription, eventValue, "", tags, categores);
	}else{
		//add item only
		var eventName=document.getElementById('eventName').value;
		var eventDescription=document.getElementById('eventDescription').value;
		var eventValue=document.getElementById('eventValue').value;
		
		if(eventName=="" || eventValue==""){
			alert("Wypelnij brakujace dane! nazwa przedmiotu, wartosc");
			return;
		}
		getMovieName("MapAdapter").insertOnlyEvent(eventLocationId, eventName, eventDescription, eventValue, "", tags, categores);
	}
}
function callbackInsertEvent(s) {
	//document.getElementById('tagbox').innerHTML = "<b>"+s+"</b>";
	document.getElementById('eventLocationName').value ="";
	document.getElementById('eventLocationAdres').value="";
	document.getElementById('eventLocationCity').value="";
	document.getElementById('eventLocationLocationName').value="";
	document.getElementById('eventLocationDescription').value="";
	document.getElementById('eventName').value="";
	document.getElementById('eventDescription').value="";
	document.getElementById('eventValue').value="";
	addLocation = false;
	getMovieName("MapAdapter").cancelEdit();
	getMovieName("MapAdapter").edit();	
	document.getElementById('setLocation').style.display="none";	
	document.getElementById('setEvent').style.display="none";	
	document.getElementById('addItemBtn').style.display="none";			
	alert(s);
}
var eventLocationId;
//map call if click at marker
function callbackClickMarker(ei) {
	eventLocationId = ei;
	document.getElementById('setLocation').style.display="none";	
	document.getElementById('setEvent').style.display="block";		
	document.getElementById('addItemBtn').style.display="block";		
	addLocation = false;	
}
/*-------------------------------------------*/
//SEARCH
var resultSearch = null;
var resultArray = new Array();
function UserOnLoad() {
	document.getElementById('resultBox').style.display="none";	
}
function search() {
	var text = document.getElementById('searchText').value;
	var valueMin = document.getElementById('valueMin').value;
	var valueMax = document.getElementById('valueMax').value;
	
	getMovieName("MapAdapter").search(text, valueMin, valueMax);
}
function searchParam(text) {
	getMovieName("MapAdapter").search(text, "", "");
}
function callbackSearch(s) {
	resultArray = [];
	resultSearch = s;
	var i = 0;
	$(resultSearch).find("event").each(function()
	{
	  	one = false;
	  	$(this).find("name").each(function()
		{
		  	name = $(this).text();
		  	if(one == false){
			  	resultArray[i] = new Array();	
			  	resultArray[i][0] = name;
			  	one=true;
		  	}
		});
		one = false;
	  	$(this).find("description").each(function()
		{
		  	description = $(this).text();
		  	if(one == false){
			  	resultArray[i][1] = description;
			  	one=true;
		  	}
		  	if(one == true){
			  	resultArray[i][4] = description;
		  	}
		});		
		one = false;
		$(this).find("value").each(function()
		{
			if(one == false){
		  		value = $(this).text();
		  		resultArray[i][2] = value;
		  		one=true;
		  	} 	
		});	
		one = false;
		$(this).find("eventlocation_id").each(function()
		{
			if(one == false){
		  		value = $(this).text();
		  		resultArray[i][3] = value;
		  		one=true;
		  	} 	
		});
		one = false;
		i++;
	});
	addItemToList();
	if(i==0){
		//document.getElementById('resultbox').innerHTML = "Brak wynikow.";
		document.getElementById('resultBox').style.display="none";	
		document.getElementById('eventDescription').innerHTML = "";			
	}else{
		document.getElementById('resultBox').style.display="block";	
		//document.getElementById('resultbox').innerHTML = "Znaleziono "+i+" wyniki.";
	}	
}
function addItemToList() {
	document.getElementById('list').innerHTML = "";
	for(i=0;i<resultArray.length;i++){
		name = resultArray[i][0];
		value = resultArray[i][2];
		document.getElementById('list').innerHTML += '<div id="listItem'+i+'" onClick="viewDescription('+i+')" ><a class="normal" style="text-decoration:none" onmouseover="this.className=\'active\'" onmouseout="this.className=\'normal\'" onclick="return false" href="">'+(i+1)+'. Nazwa: '+name+', cena: '+value+'</a></div>';
	}
}
function viewDescription(item) {

	getMovieName("MapAdapter").showEventLocatation(resultArray[item][3]);

	var s = "<br/><b>Opis:</b><br/>"; 
	s += "Nazwa leku: "+resultArray[item][0]+"<br/>"; 
	s += "Cena: "+resultArray[item][2]+" pln<br/>"; 
	s += "Opis: "+resultArray[item][1]+"<br/>"; 
	if(resultArray[item][4]!=""){
		s += "Opis apteki: "+resultArray[item][4]+"<br/>";
	}
	document.getElementById('eventDescriptionbox').innerHTML = s;
}
/*-------------------------------------------*/
//COMMON
function getMovieName(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
    } else {
        return document[movieName]
    } 
}